﻿using System;
using System.Net;
using System.Runtime.Serialization;
using System.ComponentModel;

namespace Airport
{
    [DataContract]
    public class Plane : IPlane, INotifyPropertyChanged, IEquatable<Plane>
    {
        private int _planeId;
        private string _tailNumber;
        private int _heading;
        private int _airspeed;
        private int _groundSpeed;
        private int _altitude;
        private int _bufferZone;
        private PlaneType _planeType;
        private Route _currentRoute;
        private Node _destinationNode;
        private Node _location;
        private PlaneStatus _planeStatus;
        private PlaneUpdateType _updateType;
        private Flight _planeFlight;

        public Plane()
        {
            _planeId = -1;
            _tailNumber = "";
            _heading = -1;
            _airspeed = -1;
            _groundSpeed = -1;
            _altitude = -2147483648;
            _bufferZone = -1;
            _planeType = new PlaneType();
            _currentRoute = new Route();
            _location = new Node();
            _planeStatus = PlaneStatus.UNINITIALIZED;
        }         

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }

        [DataMember]
        public int PlaneId
        {
            get
            {
                return _planeId;
            }

            set
            {
                if (_planeId != value)
                {
                    _planeId = value;
                }
            }
        }

        [DataMember]
        public string TailNumber
        {
            get
            {
                return _tailNumber;
            }

            set
            {
                if (_tailNumber != value)
                {
                    _tailNumber = value;
                }
            }
        }

        [DataMember]
        public int Heading
        {
            get
            {
                return _heading;
            }

            set
            {
                if (_heading != value)
                {
                    _heading = value;
                }
            }
        }

        [DataMember]
        public int Airspeed
        {
            get
            {
                return _airspeed;
            }

            set
            {
                if (_airspeed != value)
                {
                    _airspeed = value;
                }
            }
        }

        [DataMember]
        public int GroundSpeed
        {
            get
            {
                return _groundSpeed;
            }

            set
            {
                if (_groundSpeed != value)
                {
                    _groundSpeed = value;
                }
            }
        }

        [DataMember]
        public int Altitude
        {
            get
            {
                return _altitude;
            }

            set
            {
                if (_altitude != value)
                {
                    _altitude = value;
                }
            }
        }

        [DataMember]
        public int BufferZone
        {
            get
            {
                return _bufferZone;
            }

            set
            {
                if (_bufferZone != value)
                {
                    _bufferZone = value;
                }
            }
        }

        [DataMember]
        public PlaneType Type
        {
            get
            {
                return _planeType;
            }

            set
            {
                if (_planeType != value)
                {
                    _planeType = value;
                }
            }
        }

        [DataMember]
        public Route CurrentRoute
        {
            get
            {
                return _currentRoute;
            }

            set
            {
                if (_currentRoute != value)
                {
                    _currentRoute = value;
                }
            }
        }

        [DataMember]
        public Node DestinationNode
        {
            get
            {
                return _destinationNode;
            }

            set
            {
                if (_destinationNode != value)
                {
                    _destinationNode = value;
                }
            }
        }

        [DataMember]
        public Node Location
        {
            get
            {
                return _location;
            }

            set
            {
                if (_location != value)
                {
                    _location = value;
                    
                }
            }
        }

        [DataMember]
        public PlaneStatus Status
        {
            get
            {
                return _planeStatus;
            }

            set
            {
                if (_planeStatus != value)
                {
                    _planeStatus = value;
                }
            }
        }

        [DataMember]
        public PlaneUpdateType UpdateType
        {
            get
            {
                return _updateType;
            }

            set
            {
                if (_updateType != value)
                {
                    _updateType = value;
                }
            }
        }

        [DataMember]
        public Flight PlaneFlight
        {
            get
            {
                return _planeFlight;
            }

            set
            {
                _planeFlight = value;
            }
        }

        public bool Equals(Plane other)
        {
            // TODO Check more than just planeId

            if (other == null)
            {
                return false;
            }

            if (this._planeId != other._planeId)
            {
                return false;
            }

            return true;
        }
    }

    [DataContract]
    // This may be unneccessary
    public enum PlaneStatus
    {
        [EnumMember]
        UNINITIALIZED,
        [EnumMember]
        HOLDING,
        [EnumMember]
        TAXIING,
        [EnumMember]
        CLEARED_FOR_TAKEOFF,
        [EnumMember]
        CLEARED_FOR_LANDING,
        [EnumMember]
        TOWING,
        [EnumMember]
        LINED_UP_AND_WAITING,
        [EnumMember]
        STOPPED,
        [EnumMember]
        CRASHED,
        [EnumMember]
        CLEARED_TO_CROSS_RUNWAY
    }

    [DataContract]
    public enum PlaneUpdateType
    {
        [EnumMember]
        AddNewPlane,
        [EnumMember]
        UpdateExistingPlane,
        [EnumMember]
        DeletePlane
    }
}
